<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_CPanel_CPanel extends Controller_CPanel {
    
    public function action_index() {
        
        //Check forms
        
        switch (Arr::get($_POST, 'action', '')) {
            case 'save_changes':
                self::save_changes();
                break;
            case 'change_password':
                self::change_password();
                break;
            case 'add_language':
                self::add_language();
                break;
            default:
                break;
        }
        
        if (isset($_POST['remove_language']))
            self::remove_language();
        elseif (isset($_POST['save_language']))
            self::save_language();
        
        //Render content
        
        $this->title = __('Settings');
        $view = View::factory('cpanel/content/settings');
        
        $settings = Kohana::$config->load('app');
        
        $view->title = Arr::get($settings, 'title', '');
        $view->description = Arr::get($settings, 'description', '');
        $view->keywords = Arr::get($settings, 'keywords', '');
        $view->default_language = Arr::get($settings, 'default_language', '');
        
        $view->languages = Arr::get($settings, 'languages', array());
                
        $this->content = $view->render();
    }
    
    private function save_changes() {
        $settings = Kohana::$config->load('app');
        
        $settings->title = Arr::get($_POST, 'title');
        $settings->description = Arr::get($_POST, 'description');
        $settings->keywords = Arr::get($_POST, 'keywords');
        
        $default_language = Arr::get($_POST, 'default_language');
        if (in_array($default_language, array_keys($settings->languages))) {
            $settings->default_language = $default_language;
        } else {
            parent::add_message('Language not found!', 'warning');
        }
        
        $settings->save();
        
        parent::add_message('Settings saved!');
    }
    
    private function change_password() {
        $old_password = Auth::instance()->hash_password(Arr::get($_POST, 'old_password'));
        $new_password = Arr::get($_POST, 'new_password');
        $rpt_password = Arr::get($_POST, 'rpt_password');
        
        $user = ORM::factory('user', Auth::instance()->get_user());
        
        if ($old_password !== $user->password) {
            parent::add_message('Invalid old password!', 'error');
            return;
        }
        
        if (strlen($new_password) < 1) {
            parent::add_message('The password can not be empty!', 'error');
            return;
        }
        
        if ($new_password !== $rpt_password) {
            parent::add_message('Passwords do not match!', 'error');
            return;
        }
        
        $user->password = $new_password;
        $user->save();
        
        parent::add_message('Password saved!');
    }
    
    private function add_language() {
        $settings = Kohana::$config->load('app');
        
        $lang = Arr::get($_POST, 'lang');
        $desc = Arr::get($_POST, 'desc');
        
        if (in_array($lang, array_keys($settings->languages))) {
            parent::add_message('Language already exists!', 'error');
            return;
        }
        
        $settings['languages'][$lang] = $desc;
        $settings->save();
        
        parent::add_message('Language added!');
    }
    
    private function remove_language() {
        $settings = Kohana::$config->load('app');
        
        $language = Arr::get($_POST, 'language');
        
        if (!in_array($language, array_keys($settings->languages))) {
            parent::add_message('Language not found!', 'error');
            return;
        }
        
        $languages = $settings['languages'];
        unset($languages[$language]);
        $settings['languages'] = $languages;
        
        $settings->save();
        
        parent::add_message('Language removed!');
    }
    
    private function save_language() {
        $settings = Kohana::$config->load('app');
        
        $language = Arr::get($_POST, 'language');
        $lang = Arr::get($_POST, 'lang');
        $desc = Arr::get($_POST, 'desc');
        
        if ($language != $lang) {
            if (in_array($lang, array_keys($settings->languages))) {
                parent::add_message('Language already exists!', 'error');
                return;
            }
            
            $languages = $settings['languages'];
            unset($languages[$language]);
            $settings['languages'] = $languages;
        }
        
        $settings['languages'][$lang] = $desc;
        $settings->save();
        
        parent::add_message('Language saved!');
    }
    
}